import DatePicker from 'vue-datepicker'
import Tab from '../tab/tab.vue'
import ExportSettle from '../export-settle/export-settle.vue'
import ExportPay from '../export-pay/export-pay.vue'
import Pages from '../../../pages/pages.vue'
import getDatePickerOption from '../../../../util/date-picker-option'
import { showMsg, setMenuType, getPromoterList, importPromoterSettle } from '../../../../vuex/action'
import { getNow, getFirstTimeOfYear } from '../../../../util/moment'

export default {
  name: 'promoter-list',
  components: {
    'vue-tab': Tab,
    'vue-pages': Pages,
    'vue-date-picker': DatePicker,
    'vue-export-settle': ExportSettle,
    'vue-export-pay': ExportPay
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      seachName: '',
      startTime: getFirstTimeOfYear(),
      endTime: getNow(),
      datePickerOption: getDatePickerOption(),
      showSettle: false,
      showPay: false,
      totalCount: '',
      totalPrice: '',
      totalSettlePrice: ''
    }
  },
  vuex: {
    getters: {
      fresh: state => state.promoterListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xGetPromoterList: getPromoterList,
      xImportPromoterSettle: importPromoterSettle
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList({startTime=this.startTime, endTime=this.endTime, start=this.pages.start, limit=this.pages.limit}) {
      var params = {
        startTime: startTime,
        endTime: endTime,
        start: start,
        limit: limit
      }
      if (this.seachName) {
        params.name = this.seachName
      }
      this.xGetPromoterList(params)
        .then((data) => {
          this.list = data.list
          this.totalCount = data.totalCount
          this.totalPrice = data.totalPrice
          this.totalSettlePrice = data.totalSettlePrice
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    importData() {
      var file = this.$els.settlefile.files[0]
      if (file) {
        var formData = new FormData()
        formData.append('excelFile', file)
        this.xImportPromoterSettle(formData)
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: res.error_msg
            })
          })
      }
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
